import { createRouter, createWebHistory } from 'vue-router'
// import { useStore } from "vuex";
import {useUserStore} from "../storesPinia/user.js";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue'),
      meta: {power:[0,1,2]}
    },
    {
      path: '/quit',
      name: 'quit',
      component: () => import('../views/Quit.vue'),
      meta: {power:[0,1,2]}
    },
    {
      path: '/',
      alias: "/home",
      name: 'home',
      redirect: "/meetingroom",
      component: () => import('../views/Container.vue'),
      children:[{
        path: "meetingroom",
        name: "meetingroom",
        components:{
          default: () => import('../components/Header.vue'),
          main: () => import('../views/MeetingRoom/index.vue')
        },
        meta: {
          power: [0, 1, 2]
        }
      },{
        path: "user",
        name: "user",
        components:{
          default: () => import('../components/Header.vue'),
          main: () => import('../views/User/index.vue')
        },
        meta: {
          power: [0]
        }
      },{
        path: "team",
        name: "team",
        components:{
          default: () => import('../components/Header.vue'),
          main: () => import('../views/Team/index.vue')
        },
        meta: {
          power: [0, 1]
        }
      },{
        path: "other",
        name: "other",
        components:{
          default: () => import('../components/Header.vue'),
          main: () => import('../views/Other/index.vue')
        },
        meta: {
          power: [0, 1, 2]
        }
      },{
        path: "my",
        name: "my",
        components:{
          default: () => import('../components/Header.vue'),
          main: () => import('../views/My/index.vue')
        },
        meta: {
          power: [0, 1, 2]
        }
      }]
    }
  ]
})

// 获取当前用户的权限
// const userMsg = localStorage.getItem("userMsg") ? JSON.parse(localStorage.getItem("userMsg")) : {}
// const power = userMsg.power;

// 进入所有路由之前
router.beforeEach((to, from)=>{
  // 获取状态管理器中的权限
  // vuex
  // const store = useStore();
  // const power = store.state.user.power;

  // pinia
  const userStore = useUserStore();
  const power = userStore.power;
  
  // 获取路由元信息中的权限信息
  const routePower = to.meta.power;
  // 权限验证
  if( power && !routePower.includes(power) ){
    // console.log("拦截", from, to);
    return from.path;
  }
})

export default router
